

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="CodeHope">
  <meta name="author" content="CodeHope">
  <meta name="keywords" content="希望">
  <title>Vue实现自己的小组件 - CodeHope</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      
        
          
          
          
        
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/themes/prism-tomorrow.min.css" />
      
      
        <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.22.0/plugins/line-numbers/prism-line-numbers.min.css" />
      
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"quancundexiwang.wang","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":true,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"onlypost":true},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz","app_key":"hMuhiD4FRqhns4giqLiEH9HG","server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
  <header style="height: 80vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>CodeHope</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                村头
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                我
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('http://qiniuyun.quancundexiwang.wang/vuebg.png') repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Vue实现自己的小组件">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-07-28 14:58" pubdate>
        2020年7月28日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.6k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      25
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
            <div class="scroll-down-bar">
              <i class="iconfont icon-arrowdown"></i>
            </div>
          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Vue实现自己的小组件</h1>
            
            <div class="markdown-body">
              <h2 id="实现自己的Form表单"><a href="#实现自己的Form表单" class="headerlink" title="实现自己的Form表单"></a>实现自己的Form表单</h2><ul>
<li>指定数据</li>
<li>指定校验规则</li>
</ul>
<h3 id="Step1⃣️自定义可双向绑定的input"><a href="#Step1⃣️自定义可双向绑定的input" class="headerlink" title="Step1⃣️自定义可双向绑定的input"></a>Step1⃣️自定义可双向绑定的input</h3><div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;template&gt;
    &lt;div&gt;
        &lt;input :type&#x3D;&quot;type&quot; :value&#x3D;&quot;value&quot; @input&#x3D;&quot;inputValue&quot;&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;template&gt;

&lt;script&gt;
    export default &#123;
        name: &quot;c-input&quot;,
        props: &#123;
            type: &#123;&#x2F;&#x2F;input类型
                type: String,
                default: &quot;text&quot;
            &#125;,
            value: &#123;&#x2F;&#x2F;v-model原理就是给组件绑定value值 这个值在v-model里面绑定了
                default: &quot;&quot;
            &#125;
        &#125;,
        methods: &#123;
            inputValue(e) &#123;
                &#x2F;&#x2F;这里自定义双向绑定使 用$emit方法 直接调用 input方法
                &#x2F;&#x2F;这里的input方法来自外面v-model 传入的input方法
                &#x2F;&#x2F;v-model原理就是绑定value和@input 使用input方法改变value
                this.$emit(&quot;input&quot;, e.target.value);
            &#125;
        &#125;
    &#125;
&lt;&#x2F;script&gt;

&lt;style scoped lang&#x3D;&quot;scss&quot;&gt;
    input&#123;
        padding: 4px;
        outline: none;
        border-radius: 5px;
        transition: all .3s;
        &amp;:focus&#123;
            border: 2px solid #4db2b2;
        &#125;
    &#125;
&lt;&#x2F;style&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>‼️**$attrs在自定义组件中妙用技巧** </p>
<p>在上面我们定义的c-input组件 </p>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;c-input v-model&#x3D;&quot;userInfo.username&quot;&gt;&lt;&#x2F;c-input&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>当我们想给这个input添加一些原生input的属性例如<code>placeholder</code>，</p>
<p>因为原生input的属性很多，我们不可能通过props接受全部的属性，</p>
<p>这里我们就可以用attrs，我们从刚开始的学习中知道</p>
<p><code>$attrs当父组件向子组件传递值的时候，子组件没有使用props接受，那么这个值就会被收纳到$attrs中</code>,</p>
<p>我们可以直接向我们的自定义组件传递原生的属性值</p>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;c-input v-model&#x3D;&quot;userInfo.username&quot; placeholder&#x3D;&quot;请输入用户名！&quot;&gt;&lt;&#x2F;c-input&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>然后在组件内原声dom元素上使用v-bind展开$attrs </p>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;input :type&#x3D;&quot;type&quot;  :value&#x3D;&quot;value&quot; @input&#x3D;&quot;inputValue&quot; v-bind&#x3D;&quot;$attrs&quot;&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<p>此时的dom节点就被编译成了这个样子</p>
<div class="code-wrapper"><pre class="line-numbers language-html" data-language="html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">data-v-7fbbd8b2</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token attr-name">data-v-53895622</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>请输入用户名！<span class="token punctuation">"</span></span> <span class="token attr-name">data-v-686378aa</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">data-v-7fbbd8b2</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text<span class="token punctuation">"</span></span> <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>请输入用户名！<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></div>
<p>我们发现虽然功能完成了 但是外面的div 出现了placeholder显得有点多余此时我们可以使用属性<code>inheriAttrs</code>防止根源素属性传递。</p>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;template&gt;
    &lt;div&gt;
        &lt;input :type&#x3D;&quot;type&quot; :value&#x3D;&quot;value&quot; @input&#x3D;&quot;inputValue&quot; v-bind&#x3D;&quot;$attrs&quot;&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;template&gt;

&lt;script&gt;
    export default &#123;
        inheritAttrs: false,
        name: &quot;c-input&quot;,
        props: &#123;
            type: &#123;&#x2F;&#x2F;类型
                type: String,
                default: &quot;text&quot;
            &#125;,
            value: &#123;
                default: &quot;&quot;
            &#125;
        &#125;,
        methods: &#123;
            inputValue(e) &#123;
                this.$emit(&quot;input&quot;, e.target.value);
            &#125;
        &#125;
    &#125;
&lt;&#x2F;script&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<p>使用</p>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;c-input v-model&#x3D;&quot;userInfo.username&quot; placeholder&#x3D;&quot;请输入用户名！&quot;&gt;&lt;&#x2F;c-input&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<h3 id="Step2⃣️-简单的Form和FormItem关系"><a href="#Step2⃣️-简单的Form和FormItem关系" class="headerlink" title="Step2⃣️ 简单的Form和FormItem关系"></a>Step2⃣️ 简单的Form和FormItem关系</h3><p>FormItem </p>
<ul>
<li>Label  表单名称</li>
<li>prop  校验参数 用来获取指定的值和校验</li>
</ul>
<p>Form</p>
<ul>
<li>model Form绑定的数据</li>
<li>Rules 校验</li>
<li>全局校验</li>
</ul>
<p>第三方库async-validator</p>
<div class="code-wrapper"><pre class="line-numbers language-shell" data-language="shell"><code class="language-shell">npm install async-validator -S<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&#x2F;&#x2F;FormItem
&lt;template&gt;
    &lt;div class&#x3D;&quot;c-form-item&quot;&gt;
        &lt;!-- label标签--&gt;
        &lt;label v-if&#x3D;&quot;label&quot;&gt;&#123;&#123;label&#125;&#125;&lt;&#x2F;label&gt;
      
        &lt;slot&gt;&lt;&#x2F;slot&gt;
      
        &lt;!-- 错误校验信息--&gt;
        &lt;p v-if&#x3D;&quot;error&quot; class&#x3D;&quot;error&quot;&gt;&#123;&#123;error&#125;&#125;&lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;template&gt;

&lt;script&gt;
    import Schema from &quot;async-validator&quot;

    export default &#123;
        name: &quot;c-form-item&quot;,
        inject: [&#39;Form&#39;],
        props: &#123;
            label: &#123;
                type: String,
                default: &quot;&quot;,
            &#125;,
            prop: &#123;
                type: String
            &#125;
        &#125;,
        data() &#123;
            return &#123;
                error: &quot;&quot;,
            &#125;
        &#125;,
        methods: &#123;
            validator() &#123;
                &#x2F;&#x2F;拿到Form的rule对象中prop对应的rule
                const rule &#x3D; this.Form.rules[this.prop];
                console.log(rule)

                &#x2F;&#x2F;拿到Form的model对象中prop对应的值
                const value &#x3D; this.Form.model[this.prop];
                console.log(value)

                &#x2F;&#x2F; Schema定义规则 value:规则 生成规则的实例对象
                const schema &#x3D; new Schema(&#123;[this.prop]: rule&#125;)

                &#x2F;&#x2F;schema.validate 传入 key和value 去比对定义的规则，在callback中返回比对结果！
                &#x2F;&#x2F;schema本身是个异步，返回promise便于全局校验！
               return schema.validate(&#123;[this.prop]: value&#125;, error &#x3D;&gt; &#123;
                    if (error) &#123;
                        this.error &#x3D; error[0].message
                    &#125; else &#123;
                        this.error &#x3D; &quot;&quot;;
                    &#125;
                &#125;)
            &#125;
        &#125;,
        mounted() &#123;
            this.$on(&quot;validate&quot;, () &#x3D;&gt; &#123; &#x2F;&#x2F;监听校验事件，每次输入值的时候都调用这里的方法✅
                this.validator();
            &#125;)
        &#125;
    &#125;
&lt;&#x2F;script&gt;

&lt;style scoped&gt;
    label &#123;
        font-size: .6rem;
    &#125;
    .error &#123;
        transition: all 2s;
        font-size: .5rem;
        color: red;
    &#125;
&lt;&#x2F;style&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token function">inputValue</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token comment">//这里自定义双向绑定使 用$emit方法 直接调用 input方法</span>
    <span class="token comment">//这里的input方法来自外面v-model 传入的input方法</span>
    <span class="token comment">//v-model原理就是绑定value和@input 使用input方法改变value</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">"input"</span><span class="token punctuation">,</span> e<span class="token punctuation">.</span>target<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>$parent<span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">"validate"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>✅ <span class="token comment">//每次输入调用FormItem的 $on 监听方法 validate校验</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&#x2F;&#x2F;Form
&lt;template&gt;
    &lt;div&gt;
        &lt;slot&gt;&lt;&#x2F;slot&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;template&gt;

&lt;script&gt;
    export default &#123;
        provide() &#123;
            return &#123;
                Form: this
            &#125;
        &#125;,
        props: &#123;
            model: &#123;
                type: Object,
                default() &#123;
                    return &#123;&#125;
                &#125;,
                required: true
            &#125;,
            rules: &#123;
                type: Object,
            &#125;
        &#125;,
        methods: &#123;
            validate(cb) &#123;
                const tasks &#x3D; this.$children.filter(item &#x3D;&gt; &#123;
                    return item.prop
                &#125;).map(item &#x3D;&gt; &#123;
                    return item.validator();
                &#125;)
                console.log(tasks)
                Promise.all(tasks).then(() &#x3D;&gt; cb(true)).catch(() &#x3D;&gt; cb(false));
            &#125;
        &#125;
    &#125;
&lt;&#x2F;script&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<h2 id="2-自定义弹窗组件"><a href="#2-自定义弹窗组件" class="headerlink" title="2.自定义弹窗组件"></a>2.自定义弹窗组件</h2><h3 id="1-vue中的render函数"><a href="#1-vue中的render函数" class="headerlink" title="1.vue中的render函数"></a>1.vue中的render函数</h3><blockquote>
<p>我们先使用render函数生成虚拟dom,变成真实dom,然后在挂在到dom树中去。</p>
</blockquote>
<ul>
<li>$mount() 不能直接挂在到body,因为整个body就会变成当前渲染的组件了</li>
</ul>
<div class="code-wrapper"><pre class="line-numbers language-js" data-language="js"><code class="language-js"><span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">"vue"</span><span class="token punctuation">;</span>


<span class="token keyword">function</span> <span class="token function">create</span><span class="token punctuation">(</span><span class="token parameter">Comp<span class="token punctuation">,</span> data</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
    <span class="token keyword">const</span> vm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">&#123;</span>
        <span class="token function-variable function">render</span><span class="token operator">:</span> <span class="token parameter">h</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
            <span class="token keyword">return</span> <span class="token function">h</span><span class="token punctuation">(</span>Comp<span class="token punctuation">,</span> data<span class="token punctuation">)</span>
        <span class="token punctuation">&#125;</span>
    <span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">$mount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    document<span class="token punctuation">.</span>body<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>$el<span class="token punctuation">)</span>
    <span class="token keyword">const</span> comp <span class="token operator">=</span> vm<span class="token punctuation">.</span>$children<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

    comp<span class="token punctuation">.</span><span class="token function-variable function">remove</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
        document<span class="token punctuation">.</span>body<span class="token punctuation">.</span><span class="token function">removeChild</span><span class="token punctuation">(</span>vm<span class="token punctuation">.</span>$el<span class="token punctuation">)</span><span class="token punctuation">;</span>
        vm<span class="token punctuation">.</span><span class="token function">$destroy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token keyword">return</span> comp<span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>

<span class="token keyword">export</span> <span class="token keyword">default</span> create<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>
<div class="code-wrapper"><pre class="line-numbers language-vue" data-language="vue"><code class="language-vue">&lt;template&gt;
    &lt;div id&#x3D;&quot;c-notice&quot;&gt;
        &lt;p v-if&#x3D;&quot;isShow&quot;&gt;
            &#123;&#123;message&#125;&#125;
        &lt;&#x2F;p&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;template&gt;

&lt;script&gt;
    export default &#123;
        props: &#123;
            message: &#123;
                type: String,
            &#125;,
            autoClose: &#123;
                type: Boolean,
                default: false,
            &#125;,
            duration: &#123;
                type: Number,
                default: 3000
            &#125;
        &#125;,
        data() &#123;
            return &#123;
                isShow: true
            &#125;
        &#125;,
        methods: &#123;
            hide() &#123;
                this.isShow &#x3D; false;
                this.remove();
            &#125;
        &#125;,
        mounted() &#123;
            setTimeout(this.hide, 2000)
        &#125;,

    &#125;
&lt;&#x2F;script&gt;

&lt;style scoped lang&#x3D;&quot;scss&quot;&gt;
    #c-notice &#123;
        width: 200px;
        height: 100px;
        background-color: rgba(0, 0, 0, 0.71);
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        text-align: center;
        line-height: 100px;
        color: white;
        transform: translateY(-100px);
        transition: all 1s;
    &#125;
    .show &#123;
        transform: translateY(10px);
    &#125;
&lt;&#x2F;style&gt;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></div>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/vue%E6%A1%86%E6%9E%B6/">vue框架</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/vue%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%B7%B1%E7%9A%84%E5%B0%8F%E7%BB%84%E4%BB%B6/">vue实现自己的小组件</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/56624/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">从MVVM到Vue源码学习</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/20260/">
                        <span class="hidden-mobile">Sass</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    Fluid.utils.waitElementVisible('vcomments', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "Mi65hxq7VAFUDwOLeIGAOgiV-gzGzoHsz",
          app_key: "hMuhiD4FRqhns4giqLiEH9HG",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: false,
          recordIP: false,
          serverURLs: "",
        });
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the
    <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments powered by Valine.</a>
  </noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    

  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        豫ICP备20009912号-1
      </a>
    </span>
    
  </div>


  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":true,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  
    
  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "http://qiniuyun.quancundexiwang.wang/xmlSearch.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
